import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class CoreWechatAppService extends BaseService<any> {
  private readonly logger = new Logger(CoreWechatAppService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * app
   * 对应 PHP: CoreWechatAppService_core::app()
   * 逻辑类型: undefined - undefined
   */
  async app(site_id: number) {
    // 基于PHP真实逻辑: app
    // PHP原文: $app_config = (new CoreAppService())->getConfig($site_id);          if (empty($app_config['wechat_app_id']) || empty($app_config['wechat_app_secret'])...
const app_config = this.coreAppService.getConfig(site_id);

        if (!app_config.wechat_app_id) || !app_config.wechat_app_secret)) throw new WechatBusinessException('WECHAT_NOT_EXIST');//公众号未配置

        const config = [
            app_id: app_config.wechat_app_id,
            secret: app_config.wechat_app_secret,
            token: "",
            aes_key: 'not_encrypt',
            http: [
                timeout: 5.0,
                retry: true, // 使用默认重试配置
            )
        ];
        return new Application(config);
    }

    /**
     * 微信实例接口调用
     * @param int site_id
     * @return \EasyWeChat\Kernel\HttpClient\AccessTokenAwareClient
     * @throws InvalidArgumentBusinessException
     */
    public static function appApiClient(int site_id)
    {
        return self.app(site_id).getClient(];
    }

    /**
     * 处理授权回调
     * @param int site_id
     * @param string code
     * @return UserInterface
     */
    public static function userFromCode(int site_id, string code)
    {
        try {
            const oauth = self.app(site_id).getOauth(];
            return oauth.userFromCode(code);
  }

  /**
   * appApiClient
   * 对应 PHP: CoreWechatAppService_core::appApiClient()
   * 逻辑类型: undefined - undefined
   */
  async appApiClient(site_id: number) {
    // 基于PHP真实逻辑: appApiClient
    // PHP原文: return self::app($site_id)->getClient();     }      /**      * 处理授权回调      * @param int $site_id      * @param string $code      * @return UserInterfa...
return self.app(site_id).getClient(];
    }

    /**
     * 处理授权回调
     * @param int site_id
     * @param string code
     * @return UserInterface
     */
    public static function userFromCode(int site_id, string code)
    {
        try {
            const oauth = self.app(site_id).getOauth(];
            return oauth.userFromCode(code);
  }

  /**
   * userFromCode
   * 对应 PHP: CoreWechatAppService_core::userFromCode()
   * 逻辑类型: undefined - undefined
   */
  async userFromCode(site_id: number, code: number) {
    // 基于PHP真实逻辑: userFromCode
    // PHP原文: try {             $oauth = self::app($site_id)->getOauth();             return $oauth->userFromCode($code);...
try {
            const oauth = self.app(site_id).getOauth(];
            return oauth.userFromCode(code);
  }
}
